import TeacherModel from '../../model/TeacherModel'
import StudentModel from '../../model/StudentModel'
import MenuModel from '../../model/MenuModel'
import ErrorHandlerCode from '../../utils/ErrorHandlerCode'
import BasicController from '../BasicController'

class UserController extends BasicController{
    userQuery(req,res){

    }

	userLogin(req,res){
        let orderHeader = req.body;
		StudentModel.findById(orderHeader.userName).then((data)=>{
            if(data === null){
                TeacherModel.findById(orderHeader.userName).then((item)=>{
                    super.RequestSuccessMsg(res,item);
                }).catch((err)=>{
                    res.json(ErrorHandlerCode.INNERSERVICERROR);
                    throw err;
                })
            } else {
                if(data.password === orderHeader.password){
                    super.RequestSuccessMsg(res,data);
                } else {
                    super.RequestSuccessMsg(res,{message: '密码错误'});
                }
            }
		}).catch((err)=>{
			res.json(ErrorHandlerCode.INNERSERVICERROR);
			throw err;
		})
	}

	userLogout(req, res){
        
    }

 	userRegister(req, res){
        let orderHeader = req.body;
        let temp = {};
        if (orderHeader.usertype === 'student') {
            temp.sno = orderHeader.username;
            temp.password = orderHeader.password; 
            temp.role = 'student';
            StudentModel.add(temp).then((data) => {
                super.RequestSuccessMsg(res, data);
            }).catch((err) => {
                res.json(ErrorHandlerCode.INNERSERVICERROR);
                throw err;
            })
        } else {
            temp.tno = orderHeader.username;
            temp.password = orderHeader.password;
            temp.role = 'teacher';
            TeacherModel.add(temp).then((data) => {
                super.RequestSuccessMsg(res, data);
            }).catch((err) => {
                res.json(ErrorHandlerCode.INNERSERVICERROR);
                throw err;
            })
        }
    }

    editPassword(req, res){
        let orderHeader = req.body;
        if (orderHeader.sno !== undefined) {
            StudentModel.updatePassword(orderHeader).then((data)=>{
                super.RequestSuccessMsg(res,data);
            }).catch((err)=>{
                res.json(ErrorHandlerCode.INNERSERVICERROR);
                throw err;
            })
        } else {
            TeacherModel.updatePassword(orderHeader).then((data)=>{
                super.RequestSuccessMsg(res,data);
            }).catch((err)=>{
                res.json(ErrorHandlerCode.INNERSERVICERROR);
                throw err;
            })
        } 
        
    }

    userGetMenu(req, res){
        let orderHeader = req.body;
        MenuModel.findById(orderHeader.role).then((data)=>{
            super.RequestSuccessMsg(res,data);
        }).catch((err)=>{
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }

    fingByNo(req, res){
        let orderHeader = req.body;
        StudentModel.findById(orderHeader.userno).then((data)=>{
            if(data === null){
                TeacherModel.findById(orderHeader.userno).then((item)=>{
                    super.RequestSuccessMsg(res,item);
                }).catch((err)=>{
                    res.json(ErrorHandlerCode.INNERSERVICERROR);
                    throw err;
                })   
            } else {
                super.RequestSuccessMsg(res,data);
            }
        }).catch((err)=>{
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
}

module.exports = new UserController();